/*
 * @Author: wanli wanliofficial@aliyun.com
 * @Date: 2025-05-13 11:20:56
 * @LastEditors: wanli wanliofficial@aliyun.com
 * @LastEditTime: 2025-05-13 11:27:42
 * @FilePath: \locust4j\src\benchmark\java\com\github\myzhan\locust4j\CollectionsTest\TestArrayList.java
 * @Description: 
 * 
 * Copyright (c) 2025 by wanli email: wanliofficial@aliyun.com, All Rights Reserved. 
 */
package com.github.myzhan.locust4j.collectionstest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

// https://www.cnblogs.com/hongdada/p/13809962.html

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public class TestArrayList {
    private static final int TEN_MILLION = 10000000;

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MICROSECONDS)
    public void arrayList() {
        List<String> array = new ArrayList<>();
        for (int i = 0; i < TEN_MILLION; i++) {
            array.add("123");
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MICROSECONDS)
    public void arrayListSize() {
        List<String> array = new ArrayList<>(TEN_MILLION);
        for (int i = 0; i < TEN_MILLION; i++) {
            array.add("123");
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(TestArrayList.class.getSimpleName()).forks(1).build();
        new Runner(opt).run();
    }
}
